package com.zeei.ems.meet.util;

import org.dozer.DozerBeanMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class BeanMapperUtils {

    /**
     * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
     * <br/>DozerBeanMapper类是线程安全的.
     */
    private static DozerBeanMapper dozer;

    public static synchronized DozerBeanMapper getInstance() {
        if (dozer == null) {
            dozer = new DozerBeanMapper();
        }
        return dozer;
    }

    /**
     * 基于Dozer转换对象的类型.
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        return getInstance().map(source, destinationClass);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
        List<T> destinationList = new ArrayList<>(sourceList.size());
        for (Object sourceObject : sourceList) {
            T destinationObject = getInstance().map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    /**
     * 基于Dozer将对象A的值拷贝到对象B中.
     */
    public static void copy(Object source, Object destinationObject) {
        getInstance().map(source, destinationObject);
    }

}
